from bs4 import BeautifulSoup
import requests
from mtgprice import ioTest
import DAO


f = requests.get(
    'https://www.mtggoldfish.com/index/KLD_XLN')
print(f.status_code)
html = f.content.decode('utf-8')
# print(html)
bs = BeautifulSoup(html, "html.parser")
title = bs.title
content = bs.select('.index-price-table-paper')
content = content[0].tbody
trs = content.find_all('tr')
db = DAO.operate.connect()
for tr in trs:
    tds = tr.find_all('td')
    Card = tds[0].text.replace("'",'_')
    Set = tds[1].text.strip()
    Rarity = tds[2].text.strip()
    Price = tds[3].text.strip()
    Daily = tds[4].text.strip()
    Daily_p = tds[5].text.strip()
    Weekly = tds[6].text.strip()
    Weekly_p = tds[7].text.strip()
    insert_data = "INSERT INTO mtg VALUES ('%s','%s','%s',%s,'%s','%s','%s','%s')" %(Card,Set,Rarity,Price,Daily,Daily_p,Weekly,Weekly_p)
    DAO.operate.insert(db,insert_data)
DAO.operate.close(db)
#ioTest.out_try('C:/Users/xingran.xiong/Desktop/test.txt', content[0].prettify())